package com.ywjt.platformbusiness.home.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.cy.tablayoutniubility.FragPageAdapterVpNoScroll;
import com.cy.tablayoutniubility.TabAdapterNoScroll;
import com.cy.tablayoutniubility.TabLayoutNoScroll;
import com.cy.tablayoutniubility.TabMediatorVpNoScroll;
import com.cy.tablayoutniubility.TabNoScrollViewHolder;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.ywjt.platformbusiness.R;
import com.ywjt.platformbusiness.base.BaseActivity;
import com.ywjt.platformbusiness.common.UserData;
import com.ywjt.platformbusiness.constants.UrlConstants;
import com.ywjt.platformbusiness.home.adapter.CouponDrawAdapter;
import com.ywjt.platformbusiness.home.model.AvilibleCouponModel;
import com.ywjt.platformbusiness.http.HttpRequest;
import com.ywjt.platformbusiness.http.HttpStringCallBack;
import com.ywjt.platformbusiness.my.adapter.CouponAdapter;
import com.ywjt.platformbusiness.my.fragment.CouponFragment;
import com.ywjt.platformbusiness.store.activity.IncomeDetailActivity;
import com.ywjt.platformbusiness.store.adapter.IncomeDetailAdapter;
import com.ywjt.platformbusiness.store.model.IncomeListModel;
import com.ywjt.platformbusiness.util.JsonUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;


public class CouponDrawActivity extends BaseActivity {
    private RecyclerView rcView;
    private CouponDrawAdapter adapter;
    private SmartRefreshLayout refresh;
    private LinearLayout llNodata;



    public static void actionStart(Context context) {
        Intent intent = new Intent(context, CouponDrawActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setAndroidNativeLightStatusBar(true);
    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.fragment_order;
    }

    @Override
    protected void initView() {
        setTitleText("优惠券");
        rcView=findViewById(R.id.rcView);
        refresh=findViewById(R.id.refresh);
        rcView=findViewById(R.id.rcView);
        llNodata=findViewById(R.id.llNodata);

        rcView.setVisibility(View.VISIBLE);
        llNodata.setVisibility(View.GONE);
        getCoupon();
    }


    @Override
    protected void initData() {
        setAndroidNativeLightStatusBar(true);
    }

    @Override
    protected int getTitleBarType() {
        return 0;
    }

    private void getCoupon() {

        new HttpRequest(getContext()).doPost(UrlConstants.merchant_getIncomeDetail, "",new HttpStringCallBack() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onSuccess(Object result) {
                try {
                    String data = result.toString();
                    //把请求得数据转换成json对象
                    JSONObject jsonObject = null;
                    jsonObject = new JSONObject(data);
                    String message = jsonObject.getString("message");
                    Boolean success=jsonObject.getBoolean("success");
                    if(success){
                        Object obj = JsonUtils.fromJson(result.toString(), AvilibleCouponModel.class);
                        if (obj instanceof AvilibleCouponModel) {
                            AvilibleCouponModel model=(AvilibleCouponModel)obj;
                            adapter=new CouponDrawAdapter(model.getResult().getRecords());
                            rcView.setLayoutManager(new LinearLayoutManager(CouponDrawActivity.this));
                            rcView.setAdapter(adapter);
                        }
                    }else {
                        Toast.makeText(getContext(),message,Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int code, String msg) {
                Toast.makeText(getContext(),msg,Toast.LENGTH_SHORT).show();
            }
        });
    }

}
